package org.example.backend.Module.System.User.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.backend.Module.System.User.Mapper.UserMapper;
import org.example.backend.Module.System.User.Pojo.Entity.MyUserDetails;
import org.example.backend.Module.System.User.Pojo.Entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.security.core.userdetails.UserDetailsService;

import javax.annotation.Resource;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = this.userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));

        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        return new MyUserDetails(user);
    }
}
